﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastReport;
using FastReport.Data;
using FastReport.Data.JsonConnection;
using FastReport.Export;
using FastReport.Export.Html;
using FastReport.Export.Image;
using FastReport.Export.PdfSimple;
using FastReport.Utils;
using Microsoft.CSharp.RuntimeBinder;
using Newtonsoft.Json;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;

namespace FastReportOpenSource.WindowsFormsApp
{
    public partial class Form1 : Form
    {
        private Random _random;

        public Form1()
        {
            InitializeComponent();
            _random = new Random();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (Report report = new Report())
            {
                var page = new ReportPage();
                report.Pages.Add(page);

                page.CreateUniqueName();

                page.ReportTitle = new ReportTitleBand();
                page.ReportTitle.Height = Units.Centimeters * 1;
                page.ReportTitle.CreateUniqueName();
                page.ReportTitle.CanGrow = true;

                var textObj = new TextObject();
                textObj.Parent = page.ReportTitle;
                textObj.CreateUniqueName();
                textObj.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 1);
                textObj.Font = new Font("Arial", 14, FontStyle.Bold);
                textObj.HorzAlign = HorzAlign.Center;
                textObj.Text = "new string('A', 2048)";
                textObj.CanGrow = true;

                var band = new DataBand();
                band.Parent = page;
                band.CreateUniqueName();
                band.CanGrow = true;

                var textObj1 = new TextObject();
                textObj1.Parent = band;
                textObj1.CreateUniqueName();
                textObj1.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 1);
                textObj1.Font = new Font("Arial", 14, FontStyle.Bold);
                textObj1.HorzAlign = HorzAlign.Center;
                textObj1.Text = new string('A', 2048);
                textObj1.CanGrow = true;


                report.Prepare();
                report.Save("aaa.xml");
                var export = new ImageExport();
                export.ImageFormat = ImageExportFormat.Jpeg;
                report.Export(export, "test.jpg");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (var report = Report.FromFile("report.xml"))
            {
                var array = new int[256];
                for (int j = 0; j < array.Length; j++)
                {
                    array[j] = _random.Next(int.MinValue, int.MaxValue);
                }
                report.RegisterData(array, "Array");

                report.Prepare();
                var export = new PDFSimpleExport();
                report.Export(export, "test.pdf");
            }
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            string file = null;
            if (_currentJpgIndex == 1)
            {
                file = "test.jpg";
            }
            else
            {
                file = $"test.{_currentJpgIndex}.jpg";
            }
            _currentJpgIndex++;
            using (var img = Image.FromFile(file))
            {
                e.Graphics.DrawImage(img, 0, 0, img.Width, img.Height);
            }

            if (File.Exists($"test.{_currentJpgIndex}.jpg"))
            {
                e.HasMorePages = true;
            }
        }

        private int _currentJpgIndex;

        private void button3_Click(object sender, EventArgs e)
        {
            if (File.Exists("test.jpg"))
            {
                _currentJpgIndex = 1;
                printPreviewDialog1.ShowDialog(this);
            }
        }

        private void printDocument1_BeginPrint(object sender, PrintEventArgs e)
        {
            _currentJpgIndex = 1;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (File.Exists("test.jpg"))
            {
                _currentJpgIndex = 1;
                if (printDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    printDocument1.PrinterSettings = printDialog1.PrinterSettings;
                    printDocument1.DefaultPageSettings = printDialog1.PrinterSettings.DefaultPageSettings;
                    Activate();
                    printPreviewDialog1.ShowDialog(this);
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            pageSetupDialog1.ShowDialog(this);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var c = new List<Class1>();
            c.Add(new Class1
            {
                Id = Guid.NewGuid().ToString("N"),
                Name = "你好",
                Age = 32
            });
            c.Add(new Class1
            {
                Id = Guid.NewGuid().ToString("N"),
                Name = "师弟",
                Age = 30
            });
            File.WriteAllText("test.json", JsonConvert.SerializeObject(c, Formatting.Indented), Encoding.UTF8);
            var g = new JSchemaGenerator();
            var s = g.Generate(typeof(List<Class1>));

            File.WriteAllText("test.schema.json", s.ToString(), Encoding.UTF8);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //using (var report = Report.FromFile("未命名.frx"))
            using (var report = new 未命名())
            {
                //var c = new List<Class1>();
                //c.Add(new Class1
                //{
                //    Id = Guid.NewGuid().ToString("N"),
                //    Name = "你好",
                //    Age = 32
                //});
                //c.Add(new Class1
                //{
                //    Id = Guid.NewGuid().ToString("N"),
                //    Name = "师弟",
                //    Age = 30
                //});
                //c.Add(new Class1
                //{
                //    Id = Guid.NewGuid().ToString("N"),
                //    Name = "师兄",
                //    Age = 40
                //});
                //report.RegisterData(c, "JSON.array");

                report.Prepare();
                var export = new PDFSimpleExport();
                report.Export(export, "test.pdf");
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            using (Report report = new Report())
            {
                var ds = new BusinessObjectDataSource();
                report.Dictionary.DataSources.Add(ds);
                ds.Name = "JsonName"; // 用于报告模板里的绑定
                ds.ReferenceName = "Class1";  // 用于程序里的数据注册
                ds.DataType = typeof(Class1);
                ds.Columns.Add(new Column { Name = "Id", DataType = typeof(string) });
                ds.Columns.Add(new Column { Name = "Name", DataType = typeof(string) });
                ds.Columns.Add(new Column { Name = "Age", DataType = typeof(int) });

                var page = new ReportPage();
                report.Pages.Add(page);

                page.CreateUniqueName();

                page.ReportTitle = new ReportTitleBand();
                page.ReportTitle.Height = Units.Centimeters * 1;
                page.ReportTitle.CreateUniqueName();
                page.ReportTitle.CanGrow = true;

                var textObj = new TextObject();
                textObj.Parent = page.ReportTitle;
                textObj.CreateUniqueName();
                textObj.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 1);
                textObj.Font = new Font("Arial", 14, FontStyle.Bold);
                textObj.HorzAlign = HorzAlign.Center;
                textObj.Text = "new string('A', 2048)";
                textObj.CanGrow = true;

                var band = new DataBand();
                band.Parent = page;
                band.CreateUniqueName();
                band.CanGrow = true;

                var textObj1 = new TextObject();
                textObj1.Parent = band;
                textObj1.CreateUniqueName();
                textObj1.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 1);
                textObj1.Font = new Font("Arial", 14, FontStyle.Bold);
                textObj1.HorzAlign = HorzAlign.Center;
                textObj1.Text = new string('A', 2048);
                textObj1.CanGrow = true;


                //report.Prepare();
                report.Save("aaa.xml");
                //var export = new ImageExport();
                //export.ImageFormat = ImageExportFormat.Jpeg;
                //report.Export(export, "test.jpg");
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            using (var report = Report.FromFile("aaa.frx"))
            {
                report.SetParameterValue("Parameter", "这是一个测试报告");
                var c = new List<Class1>();
                c.Add(new Class1
                {
                    Id = Guid.NewGuid().ToString("N"),
                    Name = "你好",
                    Age = 32
                });
                c.Add(new Class1
                {
                    Id = Guid.NewGuid().ToString("N"),
                    Name = "师弟",
                    Age = 30
                });
                c.Add(new Class1
                {
                    Id = Guid.NewGuid().ToString("N"),
                    Name = "师兄",
                    Age = 40
                });
                c.Add(new Class1
                {
                    Id = Guid.NewGuid().ToString("N"),
                    Name = "八戒",
                    Age = 40
                });
                report.RegisterData(c.ToArray(), "Class1");
                // 解决找不到 Microsoft.CSharp 类库的问题
                var _ = typeof(RuntimeBinderException);
                report.Prepare();
                var export = new PDFSimpleExport();
                report.Export(export, "test.pdf");
                Process.Start("test.pdf");
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            using (var report = new Report())
            {
                report.Abort();
            }
        }
    }
}
